package com.bjpowernode.util;

import cn.hutool.json.JSONUtil;
import com.bjpowernode.result.R;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

public class ResponseUtil {

    public static void write(HttpServletResponse response, R result) {
        response.setContentType("application/json");
        PrintWriter printWriter = null;
        try {
            //把result结果对象转成json返回给前端vue页面
            String resultJSON = JSONUtil.toJsonStr(result);
            printWriter = response.getWriter();
            printWriter.write(resultJSON);
            printWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (printWriter != null) {
                printWriter.close();
            }
        }
    }
}
